package com.net.Listener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.net.domain.City;
import com.net.domain.Country;
import com.net.domain.User;
import com.net.redis.RedisService;
import com.net.service.BaseService;
import com.net.service.BrandStoreService;
import com.net.service.UserService;
import com.net.service.UserServiceImpl;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;


/**
 * Created by zhang yufei on 2017/12/7.
 */
@Service
public class StartAddCacheListener extends BaseService implements ApplicationListener<ContextRefreshedEvent> {

    //日志
    private final Logger log = Logger.getLogger(StartAddCacheListener.class);

    @Autowired
    private RedisService<Object> redisCache;
    @Autowired
    private BrandStoreService brandStoreService;
    @Autowired
    private UserServiceImpl userService;


    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        //spring 启动的时候缓存城市和国家等信息
        System.out.println(event.getApplicationContext().getDisplayName());
        if (event.getApplicationContext().getDisplayName().equals("Root WebApplicationContext")) {
            System.out.println("--------------------缓存数据--------------------");
            /*List<City> cityList = brandStoreService.selectAllCityMessage();
            List<Country> countryList = brandStoreService.selectAllCountryMessage();
            Map<Integer, City> cityMap = new HashMap<Integer, City>();
            Map<Integer, Country> countryMap = new HashMap<Integer, Country>();
            int cityListSize = cityList.size();
            int countryListSize = countryList.size();
            for (int i = 0; i < cityListSize; i++) {
                cityMap.put(cityList.get(i).getId(), cityList.get(i));
            }
            for (int i = 0; i < countryListSize; i++) {
                countryMap.put(countryList.get(i).getId(), countryList.get(i));
            }
            redisCache.setCacheIntegerMap("cityMap", cityMap);
            redisCache.setCacheIntegerMap("countryMap", countryMap);*/

            List<User> userList=getAllUser();
            Map<Integer, User> userMap = new HashMap<Integer, User>();
            int userListSize = userList.size();
            for (int i = 0; i < userListSize; i++) {
                userMap.put(userList.get(i).getId(), userList.get(i));
            }
            redisCache.setCacheIntegerMap("userMap", userMap);
        }
    }

}
